<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";
import { getCertConArea } from "@/api/charts";
const animationDuration = 6000;

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    },
    barData: {
      default: {}
    },
    loading: {
      default: true
    }
  },
  data() {
    return {
      chart: null,
      options: "",
      tableData: []
    };
  },
  watch: {
    // barData: (newValue, oldValue) => {
    //   //箭头函数  不然会发生this改变
    //   console.log("改变的值 ", newValue, oldValue);
    //    this.barData = newValue; //把新值赋值给我们的属性数据
    //     this.initChart(); //刷新echarts图表
    // },
    barData: {
      handler(newValue, oldValue) {
        console.log("oldValue", oldValue);
        console.log("newvalue", newValue);
        this.barData = newValue; //把新值赋值给我们的属性数据
        this.initChart(); //刷新echarts图表
      },
      deep: true
    }
  },
  mounted() {
    this.initChart();
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      // this.$emit('update:barData',this.barData)
      this.chart = echarts.init(this.$el, "macarons");
      let option = {
        color: ["#a4d8c2", "#ffb980", "#5ab1ef", "#2ec7c9", "#5ab1ef"],
        title: {
          backgroundColor: "#849fe2",
          textStyle: {
            color: "#fff",
            fontSize: 16
          }
        },
        trigger: "axis",
        tooltip: {
          formatter: "{b0}: {c0}"
          // formatter: function(params, ticket, callback) {
          //   console.log();
          //   getCertConArea().then(res => {
          //     callback(ticket, toHTML(content));

          //   });
          // }
        },
        xAxis: [
          {
            type: "category",
            axisLine: {
              lineStyle: {
                color: "#90979c"
              }
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitArea: {
              show: false
            },
            axisLabel: {
              interval: 0
            }
          }
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: "#90979c"
              }
            },
            offset: -10,
            axisTick: {
              show: false
            },
            axisLabel: {
              interval: 0
            },
            splitArea: {
              show: false
            }
          }
        ],
        serires: []
      };
      this.chart.setOption(option);
      this.chart.setOption(this.barData);
    }
  }
};
</script>
<style>
</style>
